package org.example.single;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            int finalI = i + 1;
            pool.submit(() -> {
                Thread.currentThread().setName("thread_" + finalI);
                System.out.printf("第%s个线程开始执行\n", finalI);
                System.out.printf("第%s个线程获取到i值为%s\n", finalI, SingleTest.getInstance().getI());
                SingleTest.getInstance().setI(finalI);
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.printf("第%s个线程获取到i值为%s\n", finalI, SingleTest.getInstance().getI());
                System.out.printf("第%s个线程执行结束\n", finalI);
            });
        }
        pool.shutdown();
        System.out.printf("main线程获取i值=%s", SingleTest.getInstance().getI());
    }
}
